package com.ningmengs789.live.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.R;
import com.ningmengs789.live.http.JsonBuilder;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.net.utils.NToast;

import org.json.JSONException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MatchActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.match_close)
    ImageView match_close;

    @BindView(R.id.match_bg)
    ImageView match_bg;

    @BindView(R.id.match_icon)
    ImageView match_icon;

    @BindView(R.id.match_tv1)
    TextView match_tv1;
    @BindView(R.id.match_tv2)
    TextView match_tv2;

    @BindView(R.id.match_voice_ll)
    LinearLayout match_voice_ll;
    @BindView(R.id.match_video_ll)
    LinearLayout match_video_ll;

    private int type = 2;
    public static boolean isResume = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_match);
        ButterKnife.bind(this);
        setHeadVisibility(View.GONE);

        type = getIntent().getIntExtra("TYPE", 2);

        match_close.setOnClickListener(this);

        if (type == 2){
            match_bg.setImageResource(R.drawable.icon_match_1);
            match_icon.setImageResource(R.drawable.icon_match_3);
            match_tv1.setText("正在缘分语音速配");
            match_voice_ll.setVisibility(View.VISIBLE);
            match_video_ll.setVisibility(View.GONE);
        }else {
            match_bg.setImageResource(R.drawable.icon_match_2);
            match_icon.setImageResource(R.drawable.icon_match_4);
            match_tv1.setText("正在缘分视频速配");
            match_voice_ll.setVisibility(View.GONE);
            match_video_ll.setVisibility(View.VISIBLE);
        }

        handler = new Handler();
        time = 0;

        setupTime();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.match_close:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        isResume = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isResume = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacks(updateTimeRunnable);
            handler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void loadData(){
        String body = "";
        try {
            body = new JsonBuilder()
                    .put("type", type)//2:语音  3:视频
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest("app/chat/filterUser", body, new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                NToast.shortToast(mContext, errInfo);
            }
            @Override
            public void onSuccess(String result) {
            }
        });
    }

    private Runnable updateTimeRunnable;
    private int time = 0;
    private final static long DELAY_TIME = 1000;
    protected Handler handler;

    public void setupTime() {
        try {
            if (updateTimeRunnable != null) {
                handler.removeCallbacks(updateTimeRunnable);
            }
            updateTimeRunnable = new UpdateTimeRunnable();
            handler.post(updateTimeRunnable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class UpdateTimeRunnable implements Runnable {

        public UpdateTimeRunnable() {

        }

        @Override
        public void run() {
            time++;

            if (time % 5 == 0){
                if (isResume){
                    loadData();
                }
            }

            int ca = time % 3;
            switch (ca){
                case 0:
                    match_tv2.setText("请稍候...");
                    break;
                case 1:
                    match_tv2.setText("请稍候.");
                    break;
                case 2:
                    match_tv2.setText("请稍候..");
                    break;
            }

            handler.postDelayed(this, DELAY_TIME);
        }
    }
}
